import Api from '../../utils/api.js';

const app = getApp();

// pages/myApproval/myApproval.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isManager: false,

    hadPppointmentList: [],
    todoPppointmentList: [],
    forwardPppointmentList: [],

    tabList: ['待审核', '已审核'],
    curTabIndex: 0 // 当前选中的tab
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.setData({
      isManager: app.globalData.userInfo.userType === '1'
    }, () => {
      if (this.data.isManager) {
        this.setData({
          tabList: ['待审核', '已审核', '已转发']
        });
      }
      this.setOrderData();
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {
    if (app.globalData.refresh_myApproval) {
      this.getApprovalList();
      app.globalData.refresh_myApproval = false;
    }
  },

  /**
   * 监听页面下拉刷新
   */
  async onPullDownRefresh () {
    await this.getApprovalList();
    wx.stopPullDownRefresh();
  },

  /* --======================数据接口======================-- */
  // 查询审批列表
  async getApprovalList () {
    const res = await Api.getMyApproves({ sessionKey: app.globalData.sessionKey });
    if (res) {
      app.globalData.myApproval = res.message;
      this.setOrderData();
    }
  },

  /* --======================功能函数======================-- */
  // 处理申请数据
  setOrderData () {
    let hads = app.globalData.myApproval.had || [];
    let todos = app.globalData.myApproval.todo || [];
    let forward = [];
    if (app.globalData.userInfo.userType === '1') {
      forward = this.addShowInfoToOrder(app.globalData.myApproval.forward);
    }
    hads = this.addShowInfoToOrder(hads);
    todos = this.addShowInfoToOrder(todos);
    this.setData({
      hadPppointmentList: hads,
      todoPppointmentList: todos,
      forwardPppointmentList: forward
    });
  },
  // 为数据添加显示信息
  addShowInfoToOrder (data) {
    const arr = data.map(item => {

      /* 1: 餐饮 2: 住宿 3: 会议 4: 车辆 5: 物业 */
      switch (item.application_type) {
      case '1':
        item = this.buildData(item,
          ['restaurant', '餐饮', '用餐人数', item.mealNum, '人', item.user_name, item.addTime]
        ); break;
      case '2':
        item = this.buildData(item,
          ['stay', '住宿', '入住人数', item.checkInNum, '人', item.user_name, item.addTime]); break;
      case '3':
        item = this.buildData(item,
          ['meeting', '会议', '会议室', item.meetingRoom, '', item.user_name, item.addTime]); break;
      case '4':
        item = this.buildData(item,
          ['car', '车辆', '车辆', item.carType, '', item.user_name, item.addTime]); break;
      case '5':
        item = this.buildData(item,
          ['property', '物业', '物业服务', item.commercialType, '', item.user_name, item.addTime]); break;
      default: break;
      }

      switch (item.current_step) {
      case '1': item.state = '待审核'; break;
      case '2': item.state = '审批中'; break;
      case '3': item.state = '已同意'; break;
      case '4': item.state = '已拒绝'; break;
      default: break;
      }
      return item;
    });
    return arr;
  },
  // 构建显示数据
  buildData (item, [typeText, type, typeInfo, value, unit, creator, createTime] = []) {
    return Object.assign({}, item, {
      typeText, type, typeInfo, value, unit, createTime, creator
    });
  },

  /* --======================事件处理======================-- */
  // 顶部tab点击处理
  handleSelectTab (e) {
    this.setData({ curTabIndex: e.currentTarget.dataset.index });
  },
  // 预约审核申请卡片点击
  handleOrderTap (e) {
    const oprType = this.data.curTabIndex === 0 ? 'approval' : 'processed';
    let curOrder = null;
    if (this.data.curTabIndex === 0) {
      curOrder = this.data.todoPppointmentList.filter(order => order.application_id === +e.currentTarget.dataset.id)[0];
    } else if (this.data.curTabIndex === 1) {
      curOrder = this.data.hadPppointmentList.filter(order => order.application_id === +e.currentTarget.dataset.id)[0];
    } else {
      curOrder = this.data.forwardPppointmentList.filter(order => order.application_id === +e.currentTarget.dataset.id)[0];
    }
    app.globalData.orderDetail = curOrder;
    wx.navigateTo({
      url: '/pages/orderDetail/orderDetail?oprType=' + oprType
    });
  }
});